package com.ruoyi.syf.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.syf.domain.RiskShutdownRecord;
import com.ruoyi.syf.domain.vo.RiskShutdownRecordVo;
import com.ruoyi.syf.mapper.RiskShutdownRecordMapper;
import com.ruoyi.syf.service.IRiskShutdownRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 开停工记录Service业务层处理
 *
 * @author yr
 * @date 2024-07-04
 */
@Service
public class RiskShutdownRecordServiceImpl extends ServiceImpl<RiskShutdownRecordMapper, RiskShutdownRecord> implements IRiskShutdownRecordService {
    @Autowired
    private RiskShutdownRecordMapper riskShutdownRecordMapper;

    /**
     * 查询开停工记录
     *
     * @param id 开停工记录主键
     * @return 开停工记录
     */
    @Override
    public RiskShutdownRecord selectRiskShutdownRecordById(Long id) {
        return riskShutdownRecordMapper.selectById(id);
    }

    /**
     * 查询开停工记录列表
     *
     * @param riskShutdownRecord 开停工记录
     * @return 开停工记录
     */
    @Override
    public List<RiskShutdownRecordVo> list(RiskShutdownRecord riskShutdownRecord) {
        return riskShutdownRecordMapper.list(riskShutdownRecord);
    }

    /**
     * 新增开停工记录
     *
     * @param riskShutdownRecord 开停工记录
     * @return 结果
     */
    @Override
    public int insertRiskShutdownRecord(RiskShutdownRecord riskShutdownRecord) {
        return riskShutdownRecordMapper.insert(riskShutdownRecord);
    }

    /**
     * 修改开停工记录
     *
     * @param riskShutdownRecord 开停工记录
     * @return 结果
     */
    @Override
    public int updateRiskShutdownRecord(RiskShutdownRecord riskShutdownRecord) {
        return riskShutdownRecordMapper.updateById(riskShutdownRecord);
    }

    /**
     * 批量删除开停工记录
     *
     * @param ids 需要删除的开停工记录主键
     * @return 结果
     */
    @Override
    public int deleteRiskShutdownRecordByIds(Long[] ids) {
        return riskShutdownRecordMapper.deleteRiskShutdownRecordByIds(ids);
    }

    /**
     * 删除开停工记录信息
     *
     * @param id 开停工记录主键
     * @return 结果
     */
    @Override
    public int deleteRiskShutdownRecordById(Long id) {
        return riskShutdownRecordMapper.deleteRiskShutdownRecordById(id);
    }

    @Override
    public void saveList(Long objectId, List<Long> unitIds, String reason) {
        List<RiskShutdownRecord> list = new ArrayList<>();
        Date date = DateUtils.getNowDate();
        for (Long unitId : unitIds) {
            list.add(new RiskShutdownRecord(objectId, unitId, date, reason));
        }
        saveBatch(list);
    }

    @Override
    public void save(Long objectId, Long unitId, String reason) {
        save(new RiskShutdownRecord(objectId, unitId, DateUtils.getNowDate(), reason));
    }

    @Override
    public void updateList(Long objectId, List<Long> unitIds) {
        LambdaQueryWrapper<RiskShutdownRecord> lqw = new LambdaQueryWrapper<RiskShutdownRecord>()
                .eq(RiskShutdownRecord::getObjectId, objectId)
                .in(RiskShutdownRecord::getUnitId, unitIds)
                .eq(RiskShutdownRecord::getEndTime, null);
        List<RiskShutdownRecord> list = baseMapper.selectList(lqw);
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(record -> record.setEndTime(DateUtils.getNowDate()));
            updateBatchById(list);
        }
    }

    @Override
    public boolean update(Long objectId, Long unitId) {
        RiskShutdownRecord shutdownRecord = riskShutdownRecordMapper.selectLastOne(objectId,unitId);
        if (shutdownRecord != null){
            shutdownRecord.setEndTime(DateUtils.getNowDate());
            baseMapper.updateById(shutdownRecord);
        }
        return true;
    }
}
